package com.zx.Ding.controller;/**
 * Author:JasonZ
 * Data:2023/2/2
 */

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zx.Ding.common.R;
import com.zx.Ding.dto.SetmealDto;
import com.zx.Ding.entity.Category;
import com.zx.Ding.entity.Setmeal;
import com.zx.Ding.service.CategoryService;
import com.zx.Ding.service.SetmealDishService;
import com.zx.Ding.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: Ding
 *
 * @description:
 *
 * @author: JasonZ
 *
 * @create: 2023-02-02 18:49
 **/

/**
 * 套餐管理
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    /*清理该缓存下所有的数据*/
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> save(@RequestBody SetmealDto setmealDto){
        log.info("套餐信息",setmealDto);
        setmealService.saveWithDish(setmealDto);

        return R.success("新增成功");
    }

    @PutMapping
//    清理该缓存下所有的数据
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> update(@RequestBody SetmealDto setmealDto){
        setmealService.updateWithDish(setmealDto);
        return R.success("修改成功");
    }

    /*
    * 查询回显套餐
    * */
    @GetMapping("/{id}")
    public R<SetmealDto> update(@PathVariable Long id){
        SetmealDto byIdWithDish = setmealService.getByIdWithDish(id);
        return R.success(byIdWithDish);
    }
    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        //分页构造器
        Page<Setmeal> pageinfo = new Page<>(page,pageSize);
        Page<SetmealDto>dtoPage=new Page<>();

        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        //添加查询条件 根据name查询
        queryWrapper.like(name!=null,Setmeal::getName,name);
        //根据时间进行排序
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(pageinfo,queryWrapper);

        //对象拷贝
        //前端显示了分类名 但是setmeal表中只有分类id 因此需要setmealdto来实现前端显示
        //page当中不需要拷贝records 用于记录数据
        BeanUtils.copyProperties(pageinfo,dtoPage,"records");
        List<Setmeal> records = pageinfo.getRecords();

        List<SetmealDto> setmealDtos =records.stream().map((item)->{
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item,setmealDto);
            Category category = categoryService.getById(item.getCategoryId());
            if (category!=null){
                //获得分类名称
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }
            return setmealDto;
        }).collect(Collectors.toList());
        dtoPage.setRecords(setmealDtos);

        return R.success(dtoPage);
    }

    @DeleteMapping
    /*清理该缓存下所有的数据*/
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String>del(@RequestParam List<Long> ids){
//        setmealService.remove(id);
        log.info("id:{}",ids);
        setmealService.removeWithDish(ids);
        return R.success("套餐删除成功");
    }

    /**
     * 批量起售
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String>sale(@PathVariable int status,@RequestParam List<Long> ids){
        //update setmeal set status=#{status} where id in (ids)
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId,ids);
        List<Setmeal> list = setmealService.list(queryWrapper);
        for (Setmeal s : list) {
            s.setStatus(status);
        }

        setmealService.updateBatchById(list);
        return R.success("更改状态成功");
    }

    /**
     * 根据分类id和启售停售状态查询出所有的套餐信息
     * 前端传来的数据是json就加上@requestBody注解
     * @param setmeal
     * @return
     */
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId+'_'+#setmeal.status")
    @GetMapping("/list")
    public R<List<Setmeal>>list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Setmeal::getStatus,1);
        queryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }
}